<?php

namespace app\store\controller\setting;

use app\store\controller\BaseController;
use app\store\service\BannerService;
use think\exception\ValidateException;
use think\facade\View;

class BannerController extends BaseController
{
    public function index()
    {
        if ($this->request->isGet()){

            $list = BannerService::getBannerList();

            if (!$list->isEmpty()){
                return View::fetch('',['list' => $list]);
            }

            return  View::fetch();
        }

        if ($this->request->isPost()){

            $param = $this->request->param('data');
            foreach ($param as $v){
                try {
                    validate('BannerValidate')->check($v);
                }catch (ValidateException $e) {
                    return json(['code' => 0, 'msg' => $e->getMessage()]);
                }
            }

           $res =  BannerService::saveBanner($param);

            return $res ? json(['code' => 1, 'msg' => '保存成功!']) : json(['code' => 0, 'msg' => '保存失败!']);
        }
    }

    public function delete()
    {
        $id = $this->request->param('id/d');
        $res = BannerService::deleteBanner($id);

        return $res ? json(['code' => 1, 'msg' => '删除成功!']) : json(['code' => 0, 'msg' => '删除失败!']);
    }

}